<?php
class DailyDeals extends Model_Abstract
{
	public function getDailyDealsOffers($siteId = null, $today = null)
	{
		$vars = array();
		$query = array();
		if($siteId !== null){
			$query[] = '(`site_id`=? OR `site_id`=0) ';
			$vars[] = $siteId;
		}
		if(!empty($today)){
			$query[] = '`start_date`<=? ';
			$vars[] = $today;
			
			$query[] = '`end_date`>=? ';
			$vars[] = $today;
		}
		if(!empty($query)){
			$sqlWhere = ' WHERE deleted=0 AND pause=0 AND ' . implode(' AND ', $query) . ' ORDER BY end_date ASC ';
		}
		
		try {
			$sql = " SELECT * FROM dailydeals_offer " . $sqlWhere;
			return $this->_connection->query($sql, $vars);
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}		
	}
	
	public function getDailyDealsOfferById ($id) {
		$sql = 'SELECT * FROM dailydeals_offer WHERE id=? AND deleted=0';
		try {
			$reuslt = $this->_connection->query($sql, array($id));
			if (count($reuslt) > 0) {
				return $reuslt[0];
			}
			return null;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
}